package com.kj.platformlogistics.widget;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.JsonObject;
import com.kj.platformlogistics.R;
import com.kj.platformlogistics.base.BaseBottomDialog;
import com.kj.platformlogistics.constants.UrlConstants;
import com.kj.platformlogistics.home.activity.SearchOrderListActivity;
import com.kj.platformlogistics.home.activity.StockListActivity;
import com.kj.platformlogistics.home.adapter.PersonSelectListAdapter;
import com.kj.platformlogistics.home.model.PersonSelectListModel;
import com.kj.platformlogistics.home.model.SolicilationedModel;
import com.kj.platformlogistics.http.HttpRequest;
import com.kj.platformlogistics.http.HttpStringCallBack;
import com.kj.platformlogistics.util.JsonUtils;
import com.kj.platformlogistics.util.SoftKeyboardUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.INPUT_METHOD_SERVICE;


/**
 * 图片上传方式选择
 */
public class PersonSelectBottomDialog extends BaseBottomDialog {
    private Context context;
    private TextView tvSure;
    private String phone="";
    private ImageView ivClose;
    private EditText etPhone;
    private RecyclerView rcCommon;
    private List<PersonSelectListModel.ResultDTO>data;
    private PersonSelectListAdapter adapter;
    private String userId="";

    private ClickListenerInterface clickListenerInterface;

    public interface ClickListenerInterface {
        void confirm(String userId);

    }

    public PersonSelectBottomDialog(Context contexts) {
        context=contexts;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.persons_dialog_bottom;
    }

    @Override
    public void bindView(View v) {
        ivClose=v.findViewById(R.id.ivClose);
        tvSure = v.findViewById(R.id.tvSure);
        etPhone=v.findViewById(R.id.etPhone);
        rcCommon=v.findViewById(R.id.rcCommon);
        data=new ArrayList();
        tvSure.setOnClickListener(new clickListener());
        ivClose.setOnClickListener(new clickListener());
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                phone=s.toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        etPhone.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                //是否是回车键
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    //隐藏键盘
                    SoftKeyboardUtils.hideSoftKeyboard((Activity) context);
                    //搜索
                    getData();
                }
                return false;
            }
        });

        getData();
    }

    public void setClicklistener(ClickListenerInterface clickListener) {
        clickListenerInterface = clickListener;
    }

    private class clickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ivClose:
                    dismiss();
                    break;
                case R.id.tvSure:
                    clickListenerInterface.confirm(userId);
                    break;
                default:
                    break;
            }
        }
    }

    //获取数据
    public void getData(){
        JSONObject jsonObject=new JSONObject();
            try {
                if (!phone.equals("")){
                jsonObject.put("phone",phone);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        new HttpRequest(context).doPost(UrlConstants.logistics_getOrderStockDriver, null,jsonObject, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), PersonSelectListModel.class);
                        if (obj instanceof PersonSelectListModel) {
                            PersonSelectListModel model=(PersonSelectListModel) obj;
                                data.clear();
                                if (model.getResult().size() > 0) {
                                    data=model.getResult();
                                    adapter=new PersonSelectListAdapter(data);
                                    rcCommon.setLayoutManager(new LinearLayoutManager(context));
                                    rcCommon.setAdapter(adapter);

                                    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                        @Override
                                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                        }
                                    });
                                    adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                        @Override
                                        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                            switch (view.getId()){
                                                case R.id.ivSelected:
                                                    for (int i=0;i<data.size();i++){
                                                        data.get(i).setSelected(false);
                                                    }
                                                    data.get(position).setSelected(true);
                                                    userId=data.get(position).getId();
                                                    adapter.notifyDataSetChanged();
                                                    break;
                                            }
                                        }
                                    });
                                }
                            }
                    }else {
                        Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }
}